package guiCode;

import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.sun.rowset.CachedRowSetImpl;

import secretary.closeSecretaryScreen;
import secretary.defineSemester;

import flexibility.defineUserRole;
import flexibility.uploadFileInfo;
import flexibility.userData;
import gui.uploadManager;
import basic.ClientConsole;

public class studentScreenCode {

 
 private static Integer myID; //Stores the user's ID
 private static ClientConsole studentConnection;
 public uploadManager uploadMngr;
 private checkDataBase busy = new checkDataBase();
 
 public void runUploadManager(){
  uploadMngr=new uploadManager(studentConnection,myID,busy);

  System.out.print("Before getFile");
  Integer sof=uploadManager.UM_fileID;
  System.out.print(sof);
 }
 
 /*public Integer getFileID(){
  System.out.println("in while "+ uploadManager.UM_fileID);
  while (busy.getBusy() == false) {
   
  }
  System.out.println("afer while"+uploadManager.UM_fileID);
  return uploadManager.UM_fileID;
 */
  
 
 //Stores the user's ID
 public void setMyID(int id) {
  myID = id;
 }
 
 //Stores the connection to the server
 public void setConnection(ClientConsole conn){
  studentConnection = conn;
 }
}

	static Integer myID; // Stores the user's ID
	private static ClientConsole studentConnection;
	public uploadManager uploadMngr;
	public uploadFileInfo fileInfo;
	public Integer assignmentID = 3;
	static Integer currentSemesterID;
	private CachedRowSetImpl crs;
	private userData DBData = new userData();

	/**
	 * This method runs when the user press "Upload Assignment"
	 */
	public void runUploadManager() {
		Integer fileID;
		fileInfo = new uploadFileInfo(); // Initialize fileInfo
		fileID=ifCommited(assignmentID, myID, currentSemesterID);
		System.out.println("The existed fileID ="+fileID);
		if (fileID!=null) {
			int n = JOptionPane.showConfirmDialog(
				    null,
				    "You have commited this assignment! \n"+"Do you want to replace it?",
				    "Uploading status",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
			if (n==1){
				return;
			}

			fileInfo.setIfUpdate(true);
			fileInfo.setfileID(fileID);
		} else {
			fileInfo.setIfUpdate(false);
		}
		fileInfo.setAssignmentID(assignmentID);
		fileInfo.setRole(4);
		fileInfo.setAssignmentCommitDate(this.getCurrentDate());
		fileInfo.setSemesterID(currentSemesterID);
		uploadMngr = new uploadManager(studentConnection, myID, fileInfo);

	}

	// Stores the user's ID
	public void setMyID(int id) {
		myID = id;
	}
	
	public Integer getMyID() {
		return myID;
	}

	public void setCurrentSemester(int id) {
		currentSemesterID = id;
	}

	// Stores the connection to the server
	public void setConnection(ClientConsole conn) {
		studentConnection = conn;
	}

	/**
	 * This method checks if the specific assignment was committed before
	 */
	public Integer ifCommited(Integer assignmentID, Integer studentID,
			Integer currentSemesterID) {

		Integer fileID = null;

		DBData.deleteAll();
		DBData.addData(46); // query to check if the specific assignment was
							// committed by student
		DBData.addData(1);
		DBData.addData(assignmentID);
		DBData.addData(studentID);
		DBData.addData(currentSemesterID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();// this will get the results from
												// the server

		try {
			if (crs.next()) {
				fileID=crs.getInt(1);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return fileID;
	}

	/**
	 * This method returns the current date according to server
	 * 
	 */
	public String getCurrentDate() {
		String CurrentDate = null;
		DBData.deleteAll();
		DBData.addData(44);
		DBData.addData(1);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();// this will get the results from
												// the server
		try {
			crs.next();
			CurrentDate = crs.getString(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return CurrentDate;

	}
	
	/**
	 * This method executes logout and closes the student's window
	 * @return 
	 */
	public void closeStudentWindow(){
		
		closeSecretaryScreen css = new closeSecretaryScreen();
		css.closeSecretearyScreen( myID, studentConnection, DBData);
		
	}
	
	/**
	 * 
	 * @return String [] nameArr - String array of FirstName and SecondName of the student
	 */
	public String[] getStudentName(){
		String [] nameArr = new String[2];
		DBData.deleteAll();
		DBData.addData(172);
		DBData.addData(1);
		DBData.addData(myID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		try {
			crs.next();
			nameArr[0] = crs.getString(1);
			nameArr[1] = crs.getString(2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nameArr;
	}
	
	/**
	 * This method returns class name of the student
	 */
	public String getClassName(){
		String className=null;
		DBData.deleteAll();
		DBData.addData(48);
		DBData.addData(1);
		DBData.addData(myID);
		DBData.addData(currentSemesterID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		try {
			crs.next();
			className = crs.getString(1);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return className;
	}
	
	
}

